<?php
namespace app\common\model;
use app\common\cache\CacheData;

class AgentTypeLevelCoinConfig extends Base
{
	public function cacheData(){
		$data = $this -> select();
		if($data){
			foreach($data as &$value):
				$value = $value->toArray();
			endforeach;
		}
		return $data;
	}

	/**
     * 代理类型等级货币配置缓存
     * @param  [type] $id [description]
     * @return [type]             [description]
     */
    public static function agentTypeLevelCoinConfigData($id=null){
        $CACHE = CacheData::getInstance('AgentTypeLevelCoinConfig');
        // $CACHE -> updateCache();
        $cacheData = $CACHE -> readCache();
        $data = [];
        foreach($cacheData as $cache):
            $cache['level_name'] = Level::levelData($cache['level_id'])['level_name'];
            $cache['coin_name'] = Coin::coinData($cache['coin_id'])['coin_name'];
            $cache['agent_type_name'] = AgentType::agentTypeData($cache['agent_type_id'])['type_name'];
            $data[$cache['id']] = $cache;
        endforeach;
        if(!is_null($id)){
            if(isset($data[$id])):
                return $data[$id];
            else:
                return false;
            endif;
        }else{
            return $data;
        }
    }

    /**
     * 代理类型配置查询
     * $id 为代理类型id
     * [query_agent_type_config description]
     * @return [type] [description]
     */
    public static function agentTypeConfig($id=null){
        $configData = self::agentTypeLevelCoinConfigData();
        if(is_null($id))
            return $configData;
        // 所有代理类型id为$id的数据
        $typeData = sameKeyValueArray($configData,['agent_type_id'=>$id]);
        // 按照level_id分组
        $data = [];
        foreach($typeData as $value):
            $value['coin_name'] = Coin::coinData($value['coin_id'])['coin_name'];
            $data[$value['level_id']][] = $value;
        endforeach;
        // 所有等级数据
        $levelsData = Level::levelData();
        // 将配置组合进等级
        $returnData = [];
        foreach($levelsData as $level):
            $level['config'] = isset($data[$level['id']])?$data[$level['id']]:[];
            $returnData[$level['id']] = $level;
        endforeach;
        return $returnData;
    }
    

}
